define(function (require) {
    var doT = require('doT');
    require('table');
    require('treegrid');
    require('bootstrapValidator');
    require('messager');
    require('typeahead');
    require('doctor-select');
    require('doctor-data');
    require('fileinput');
    require('daterangepicker');
    require('moment');
    require('clockpicker');
    require('iCheck');
    require('jquery.ztree');

    $.module("BASE.ProfitprofitReportItem", function () {

        return {
            init: function () {
                // this.loadData();

                this.getProfitTableTemplateDetail();
                this.loadEvent();
            },
            clearDatas:function ()  {
                let that = this;

                $("#_id").val(0);
                $("#Name").val('');
                $("#ShowName").val('');
                $("#TemplateId").val('').removeAttr('data-id');
                $("#ParentID").val('').removeAttr('data-id');

                $("#VisOrder").val(11);
                $("input[name='Type'][value='10']").iCheck('check');
                $("input[name='CalMethod'][value='-1']").iCheck('check');
                $('#fm_accountview #AccountViewCode').val('').removeAttr('data-id');

                $('#fm_formula #FormulaReportItemId').val('').removeAttr('data-id');
                $("#fm_formula input[name='FormulaCalMethod'][value='*1']").iCheck('check');

                $('#fm').bootstrapValidator('resetForm', true);
                $('#fm_accountview').bootstrapValidator('resetForm', true);
                $('#fm_formula').bootstrapValidator('resetForm', true);
                that.showDiv('main_div');
            },
            getProfitTableTemplateDetail(){
                var that = this;

                //根据id获取菜单信息
                SYS.Core.ajax({
                    url: "base/profitTableTemplate/active",
                    common: {},
                    data: {},
                    success: function (data) {
                        if(data.code == 1){
                            //填充表单
                            let templateInfo = data.data;
                            $('#s_TemplateId').val(templateInfo.Name);
                            $('#s_TemplateId').attr('data-id', templateInfo.Id);

                            if (templateInfo.Id) {
                                // 默认查询
                                that.loadData(templateInfo.Id);
                            }
                        }
                    }
                });
            },
            loadData: function (templateId, showAll=false) {
                var that = this;

                SYS.Core.ajax({
                    url: 'base/profitReportItem/treeList',
                    common: {},
                    data: {
                        TemplateId: templateId
                    },
                    success: function (data) {
                        let template = doT.template($("#tmpl_tree_data_list").html());
                        $("#tree_data_list").html(template(data.data));

                        $('#org_tree').treegrid();
                        if (showAll){
                            $('#org_tree').treegrid('expandAll');
                        } else {
                            $('#org_tree').treegrid('collapseAll');
                        }
                    }
                })

                // let s_id = $("#s_id").val();
                // let s_name = $("#s_name").val();
                //
                // // let s_create_time_begin = $("#s_create_time_begin").val();
                // // let s_create_time_end = $("#s_create_time_end").val();
                //
                // SYS.Core.ajax({
                //     url: "base/profitReportItem/list",
                //     common: {
                //         page: pageNumber ? pageNumber : 1,
                //         size: pageSize ? pageSize : 20
                //     },
                //     data: {
                //         Id: s_id || 0,
                //         Name: s_name
                //     },
                //     success: function (data) {
                //         let obj = {
                //             'pageNumber': data.data.extra.current_page,
                //             'pageSize': data.data.extra.page_size,
                //             'totalRows': data.data.extra.all_count,
                //             'data': data.data.list
                //         };
                //
                //         $("#myTable").bootstrapTable('destroy');
                //         $('#myTable').bootstrapTable({
                //             striped: true,
                //             singleSelect: false,
                //             showColumns: true,
                //             showToggle: true,
                //             pagination: true,
                //             pageNumber: obj.pageNumber,
                //             pageSize: obj.pageSize,
                //             totalRows: obj.totalRows,
                //             pageList: [20, 50, 100, 200],
                //             sidePagination: 'server',
                //             clickToSelect: true,
                //             idField: 'id',
                //             data: obj.data,
                //             columns: [
                //                 // {field: 'checked', checkbox: true} ,
                //                 {field: 'Id', title: '报表项目编号', align: 'center'},
                //                 {field: 'Name', title: '报表项目名称', align: 'center'},
                //                 {field: 'TemplateId', title: '利润模版', align: 'center',
                //                     formatter: function (value, row){
                //                         let html = '';
                //                         let templateInfo = row.TemplateInfo;
                //                         if (value > 0) {
                //                             html = templateInfo.Name;
                //                         }
                //                         return html;
                //                     }
                //                 },
                //                 {field: 'ParentID', title: '父级报表项目', align: 'center',
                //                     formatter: function (value, row){
                //                         let html = '';
                //                         let parentInfo = row.ParentInfo;
                //                         if (value > 0) {
                //                             html = parentInfo.Name;
                //                         }
                //                         return html;
                //                     }
                //                 },
                //                 {field: 'VisOrder', title: '排序号', align: 'center'},
                //                 {field: 'Type', title: '类型', align: 'center',
                //                     formatter: function (value, row){
                //                         let html = '';
                //                         if (value == 10) {
                //                             html = '科目汇总';
                //                         } else if(value == 20) {
                //                             html = '公式';
                //                         }
                //                         return html;
                //                     }
                //                 },
                //                 // {field: 'count', title: '报表项关联的科目', align: 'center',
                //                 //     formatter:function (val, row){
                //                 //         return val;
                //                 //     }
                //                 // },
                //                 {
                //                     field: 'do', title: '操作', align: 'center',
                //                     formatter: function (value, row, index) {
                //                         var html = '';
                //                         html += '<a href="javascript:void(0)" onclick="BASE.ProfitprofitReportItem.toEdit(' + row.Id + ')" class="btn btn-edit">编辑</a> | ';
                //                         html += '<a href="javascript:void(0)" onclick="BASE.ProfitprofitReportItem.toProfitReportItemDetail1(' + row.Id + ')" class="btn btn-search">相关科目</a> | ';
                //                         html += '<a href="javascript:void(0)" onclick="BASE.ProfitprofitReportItem.toProfitReportItemDetail2(' + row.Id + ')" class="btn btn-search">相关公式</a>';
                //                         return html;
                //                     }
                //                 }
                //             ],
                //             onPageChange: function(number, size){
                //                 that.loadData(number, size);
                //             },
                //             onRefreshTable: function () {
                //                 that.loadData();
                //             },
                //             onClickCell: function (field, value, row, $element) {
                //             }
                //         });
                //     }
                // });
            },
            loadProfitReportItemDetail1Data: function (reportItemId, pageNumber, pageSize) {
                var that = this;

                $("#indexMask").show();

                let s_id = $("#s_id").val();
                let s_name = $("#s_name").val();

                // let s_create_time_begin = $("#s_create_time_begin").val();
                // let s_create_time_end = $("#s_create_time_end").val();

                SYS.Core.ajax({
                    url: "base/profitReportItemDetail1/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        ReportItemId: reportItemId
                    },
                    success: function (data) {
                        let obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };
                        $("#indexMask").hide();

                        $("#myTable1").bootstrapTable('destroy');
                        $('#myTable1').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            // clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                // {field: 'checked', checkbox: true} ,
                                {field: 'Id', title: '编号', align: 'center'},
                                {field: 'ReportItemId', title: '报表项目信息', align: 'center',
                                    formatter: function (value, row){
                                        let html = '';
                                        if (value > 0) {
                                            let reportItemName = row.ReportItemName || "";
                                            html = reportItemName + "(" + value + ")";
                                        }
                                        return html;
                                    }
                                },
                                {field: 'AccountViewCode', title: '科目信息', align: 'center',
                                    formatter: function (value, row){
                                        let html = '';

                                        if (value) {
                                            let accountViewCode = row.AccountViewCode || "";
                                            let accountViewName = row.AccountViewName || "";
                                            html = accountViewCode + "-" + accountViewName;
                                        }
                                        return html;
                                    }
                                },
                                {
                                    field: 'do', title: '操作', align: 'center',
                                    formatter: function (value, row, index) {
                                        return '<a href="javascript:void(0)" onclick="BASE.ProfitprofitReportItem.toProfitReportItemDetail1Remove(' + row.Id + ',' + reportItemId + ')" class="text-do-remove"><i class="fa fa-trash"></i> 删除</a>';
                                    }
                                }
                            ],
                            onPageChange: function(number, size){
                                that.loadProfitReportItemDetail1Data(reportItemId,number, size);
                            },
                            onRefreshTable: function () {
                                that.loadProfitReportItemDetail1Data(reportItemId);
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadProfitReportItemDetail2Data: function (reportItemId, pageNumber, pageSize) {
                var that = this;

                $("#indexMask").show();

                let s_id = $("#s_id").val();
                let s_name = $("#s_name").val();

                // let s_create_time_begin = $("#s_create_time_begin").val();
                // let s_create_time_end = $("#s_create_time_end").val();

                SYS.Core.ajax({
                    url: "base/profitReportItemDetail2/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        ReportItemId: reportItemId
                    },
                    success: function (data) {
                        let obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };
                        $("#indexMask").hide();

                        $("#myTable2").bootstrapTable('destroy');
                        $('#myTable2').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            // clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                // {field: 'checked', checkbox: true},
                                {field: 'Id', title: '编号', align: 'center'},
                                {field: 'ReportItemId', title: '报表项目信息', align: 'center',
                                    formatter: function (value, row){
                                        let html = '';
                                        if (value > 0) {
                                            let reportItemName = row.ReportItemName;
                                            html = reportItemName + "(" + value + ")";
                                        }
                                        return html;
                                    }
                                },
                                {field: 'FormulaReportItemId', title: '关联的报表项目', align: 'center',
                                    formatter: function (value, row){
                                        let html = '';
                                        if (value > 0) {
                                            let formulaReportItemName = row.FormulaReportItemName;
                                            html = formulaReportItemName + "(" + value + ")";
                                        }
                                        return html;
                                    }
                                },
                                {field: 'FormulaCalMethod', title: '运算管理', align: 'center'},
                                {
                                    field: 'do', title: '操作', align: 'center',
                                    formatter: function (value, row, index) {
                                        return '<a href="javascript:void(0)" onclick="BASE.ProfitprofitReportItem.toProfitReportItemDetail2Remove(' + row.Id + ',' + reportItemId + ')" class="text-do-remove"><i class="fa fa-trash"></i> 删除</a>';
                                    }
                                }
                            ],
                            onPageChange: function(number, size){
                                that.loadOrgFeeCompanyCostCenterData(reportItemId,number, size);
                            },
                            onRefreshTable: function () {
                                that.loadOrgFeeCompanyCostCenterData(reportItemId);
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    let val = $('#s_TemplateId').val();
                    let id = $('#s_TemplateId').attr('data-id');
                    if (val == "") {
                        $.messager.popup("请选择利润模版，在进行查询", "warn");
                        return;
                    }

                    that.loadData(id);
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        that.loadData();
                    }
                });

                // 初始化搜索日期控件
                //SYS.Tool.initDaterangSearchWithHHmm('s_hx_time', 's_hx_time_begin', 's_hx_time_end');
                //SYS.Tool.initDaterangSearchWithHHmm('s_create_time', 's_create_time_begin', 's_create_time_end');


                var typeAheadArr = [
                    {id: "s_ishx", data: DOCTOR.Data.common.isuse_status}
                ];
                for (let i in typeAheadArr) {
                    let typeHead = typeAheadArr[i];
                    SYS.Tool.selectTypeahead(typeHead.id, typeHead.data);
                }

                // 利润模版
                $("#s_TemplateId").on('click', function () {
                    DOCTOR.Select.showProfitTableTemplate(this);
                });
                // 报表项目父级
                $("#ParentID").on('click', function () {
                    DOCTOR.Select.showProfitReportItem(this);
                });

                // 批量删除
                $("#btn_deletes").on('click',function(){
                    that.toRemoves();
                });

                // 绑定日期控件
                // $("#creaetdate, #hxdate").datetimepicker({
                //     minView: "hour",
                //     format: 'yyyy-mm-dd hh:ii',
                //     todayBtn: true,
                //     language: 'zh-CN', // 汉化
                //     autoclose: true    // 选择日期后自动关闭
                // }).on('changeDate', function (ev) {
                //     var field = $(this).attr("id");
                //     $form.find("input[data-field='" + field + "']").val(ev.date.getTime());
                // });

                // 新增
                $("#btn_add").on('click', function () {
                    that.clearDatas();

                    $("#btn-add-div").show();
                    $("#btn-edit-div").hide();
                    that.showDiv('edit_div');
                });


                // 科目
                $("#AccountViewCode").on('click', function () {
                    DOCTOR.Select.showAccountView(this);
                });
                // 报表
                $("#FormulaReportItemId").on('click', function () {
                    DOCTOR.Select.showProfitReportItem(this);
                });

                // 新增科目
                $("#btn_add_accountview").on('click', function () {
                    that.clearDatas();

                    $('#myModal1').modal('show');
                    that.showDiv('accountview_div');
                });

                // 新增成本
                $("#btn_add_formula").on('click', function () {
                    that.clearDatas();

                    $('#myModal2').modal('show');
                    that.showDiv('formula_div');
                });

                $(".i-checks").iCheck({checkboxClass:"icheckbox_square-orange", radioClass:"iradio_square-orange"});

                // 返回
                $(".back_list").on('click', function () {
                    that.clearDatas();

                    that.showDiv('main_div');
                });

                //
                $('#fm').bootstrapValidator({
                    excluded: ':disabled',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        Name: {
                            validators: {
                                notEmpty: {
                                    message: '此项必填'
                                }
                            }
                        },
                        // VisOrder: {
                        //     validators: {
                        //         // notEmpty: {
                        //         //     message: '此项必填'
                        //         // },
                        //         regexp: {
                        //             regexp: /^[1-9]\d*$/i,
                        //             message: '格式不正确，必须大于0'
                        //         }
                        //     }
                        // }
                    }
                }).on('success.form.bv', function (e) {
                    e.preventDefault();

                    that.save();
                });

                $('#fm_accountview').bootstrapValidator({
                    excluded: ':disabled',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        // Name: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         }
                        //     }
                        // },
                    }
                }).on('success.form.bv', function (e) {
                    e.preventDefault();
                    let val = $("#AccountViewCode").val();
                    let Id = $("#AccountViewCode").attr('data-id');
                    if(Id == "" && val == "") {
                        $.messager.popup("请选择一个科目后，在提交", 'error');
                        $('#fm_accountview').bootstrapValidator('disableSubmitButtons', false);
                        return;
                    }
                    that.saveProfitReportItemDetail1();
                });

                $('#fm_formula').bootstrapValidator({
                    excluded: ':disabled',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        // Name: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         }
                        //     }
                        // },
                    }
                }).on('success.form.bv', function (e) {
                    e.preventDefault();
                    let val = $("#FormulaReportItemId").val();
                    let Id = $("#FormulaReportItemId").attr('data-id');
                    if(Id == "" && val == "") {
                        $.messager.popup("请选择一个关联的报表项目，在提交", 'error');
                        $('#fm_formula').bootstrapValidator('disableSubmitButtons', false);
                        return;
                    }
                    that.saveProfitReportItemDetail2();
                });

            },
            save: function () {
                var that = this;

                let id = $("#_id").val();
                let Name = $("#Name").val();
                let ShowName = $("#ShowName").val();
                let TemplateId = $("#TemplateId").val();
                let ParentId = $("#ParentId").val();
                let VisOrder = $("#VisOrder").val();
                let Level = $("#Level").val();
                let Type = $("input[name='Type']:checked").val()
                let CalMethod = $("input[name='CalMethod']:checked").val()

                SYS.Core.ajax({
                    url: 'base/profitReportItem/save',
                    common: {},
                    data:{
                        Id: id || 0,
                        Name: Name,
                        ShowName: ShowName,
                        TemplateId: TemplateId || 0,
                        ParentId: ParentId || 0,
                        VisOrder: VisOrder || 0,
                        Level: Level,
                        Type: Type,
                        CalMethod: CalMethod
                    },
                    success: function (data) {
                        if(data.code == 1) {
                            $.messager.popup(data.msg, 'success');

                            that.loadData(TemplateId, true);
                            that.showDiv('main_div');
                        } else {
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup(data.msg, 'error');
                        }
                    }
                })
            },
            saveProfitReportItemDetail1: function () {
                var that = this;

                let ReportItemId = $("#ReportItemId").val();
                let AccountViewCode = $("#AccountViewCode").attr('data-id');

                SYS.Core.ajax({
                    url: 'base/profitReportItemDetail1/save',
                    common: {},
                    data:{
                        Id: 0,
                        ReportItemId: ReportItemId || 0,
                        AccountViewCode: AccountViewCode
                    },
                    success: function (data) {
                        if(data.code == 1) {
                            $.messager.popup(data.msg, 'success');

                            that.loadProfitReportItemDetail1Data(ReportItemId);
                            $('#myModal1').modal('hide');
                        } else {
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup(data.msg, 'error');
                        }
                    }
                })
            },
            saveProfitReportItemDetail2: function () {
                var that = this;

                let ReportItemId = $("#ReportItemId2").val();
                let FormulaReportItemId = $("#FormulaReportItemId").attr('data-id');
                let FormulaCalMethod = $("input[name='FormulaCalMethod']:checked").val()

                SYS.Core.ajax({
                    url: 'base/profitReportItemDetail2/save',
                    common: {},
                    data:{
                        Id: 0,
                        ReportItemId: ReportItemId,
                        FormulaReportItemId: FormulaReportItemId,
                        FormulaCalMethod: FormulaCalMethod
                    },
                    success: function (data) {
                        if(data.code == 1) {
                            $.messager.popup(data.msg, 'success');

                            that.loadProfitReportItemDetail2Data(ReportItemId);
                            $('#myModal2').modal('hide');
                        } else {
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup(data.msg, 'error');
                        }
                    }
                })
            },
            addChild: function (id, TemplateId="", Level= "") {
                let that = this;
                that.clearDatas();

                if (id === 0) {
                    let val = $("#s_TemplateId").val() || "";
                    TemplateId = $("#s_TemplateId").attr('data-id') || "";

                    if(TemplateId == "" && val == "") {
                        $.messager.popup("搜索条件请选择一个关联利润模版，在添加", 'error');
                        $('#fm').bootstrapValidator('resetForm', true);
                        return;
                    }
                }
                $("#fm input[name='ParentId']").val(id); //绑定父菜单ID
                $("#fm input[name='TemplateId']").val(TemplateId); //绑定父菜单ID

                if(Level == ""){
                    $("#fm input[name='Level']").val(0);                  // 节点所在层级
                } else {
                    $("#fm input[name='Level']").val(Number(Level) + 1);  // 节点所在层级
                }

                $("#btn-add-div").show();
                $("#btn-edit-div").hide();

                that.showDiv('edit_div');
            },
            toEdit: function (id) {
                var that = this;
                that.clearDatas();

                $("#btn-add-div").hide();
                $("#btn-edit-div").show();

                SYS.Core.ajax({
                    url: 'base/profitReportItem/detail',
                    common: {},
                    data: {
                        Id: id || 0
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            var obj = data.data;

                            const id = obj.Id;
                            const Name = obj.Name;
                            const ShowName = obj.ShowName;
                            const TemplateId = obj.TemplateId;
                            const TemplateInfo = obj.TemplateInfo;

                            const ParentId = obj.ParentId;
                            const VisOrder = obj.VisOrder;
                            const Type = obj.Type;
                            const Level = obj.Level;
                            const CalMethod = obj.CalMethod;

                            $("#_id").val(id);
                            $("#Name").val(Name);
                            $("#ShowName").val(ShowName);
                            $("#ParentId").val(ParentId);
                            $("#VisOrder").val(VisOrder);
                            $("#TemplateId").val(TemplateId);
                            $("#Level").val(Level);
                            $("input[name='Type'][value='" + (Type != null ? Type : 0) + "']").iCheck('check');
                            $("input[name='CalMethod'][value='" + (CalMethod != null ? CalMethod : -1) + "']").iCheck('check');

                            that.showDiv('edit_div');
                        }
                    }
                });
            },
            toProfitReportItemDetail1: function (id) {
                var that = this;

                // 设置reportItemId
                $("#ReportItemId").val(id);
                $("#myTable1").bootstrapTable('destroy');

                this.loadProfitReportItemDetail1Data(id);
                that.showDiv('accountview_div');

                // SYS.Core.ajax({
                //     url: 'base/profitReportItem/detail',
                //     common: {},
                //     data: {
                //         Id: id || 0
                //     },
                //     success: function (data) {
                //         if (data.code == 1) {
                //             var obj = data.data;
                //
                //             that.showDiv('edit_div');
                //         }
                //     }
                // });
            },
            toProfitReportItemDetail2: function (id) {
                var that = this;

                // 设置reportItemId
                $("#ReportItemId2").val(id);
                $("#myTable2").bootstrapTable('destroy');

                this.loadProfitReportItemDetail2Data(id);
                that.showDiv('formula_div');

                // SYS.Core.ajax({
                //     url: 'base/profitReportItem/detail',
                //     common: {},
                //     data: {
                //         Id: id || 0
                //     },
                //     success: function (data) {
                //         if (data.code == 1) {
                //             var obj = data.data;
                //
                //             that.showDiv('edit_div');
                //         }
                //     }
                // });
            },
            toProfitReportItemDetail1Remove: function (id, profitReportItemId) {
                var that = this;
                $.messager.confirm("确定要删除吗？", function() {
                    SYS.Core.ajax({
                        url: "base/profitReportItemDetail1/delete",
                        common: {},
                        data: {
                            ids: [id]
                        },
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, "success");
                                that.loadProfitReportItemDetail1Data(profitReportItemId);
                            }else{
                                $.messager.popup(data.msg,'error');
                            }
                        }
                    })
                });
            },
            toProfitReportItemDetail2Remove: function (id, profitReportItemId) {
                var that = this;
                $.messager.confirm("确定要删除吗？", function() {
                    SYS.Core.ajax({
                        url: "base/profitReportItemDetail2/delete",
                        common: {},
                        data: {
                            ids: [id]
                        },
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, "success");
                                that.loadProfitReportItemDetail2Data(profitReportItemId);
                            }else{
                                $.messager.popup(data.msg,'error');
                            }
                        }
                    })
                });
            },
            delete: function (id) {
                var that = this;

                let TemplateId = $('#s_TemplateId').attr('data-id');
                $.messager.confirm('确定要删除选择的利润表项目吗？如果存在子利润表项目，将一并删除？', function () {
                    SYS.Core.ajax({
                        url: "base/profitReportItem/delete",
                        common: {},
                        data: {Id: id},
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, 'success');
                                that.loadData(TemplateId, true);
                            }else{
                                $.messager.popup('删除失败，请稍后再试', 'error');
                            }
                        }
                    })
                });
            },
            toRemoves: function () {
                var that = this;
                var selRows = $('#myTable').bootstrapTable('getSelections');
                var delIds = [];
                if(selRows.length > 0){
                    $.each(selRows, function (i, obj) {
                        if(obj.id > 0){
                            delIds.push(obj.id);
                        }
                    });
                    $.messager.confirm("确定批量删除吗？", function() {
                        SYS.Core.ajax({
                            url: "base/profitReportItem/delete",
                            common: {},
                            data: {
                                ids: delIds
                            },
                            success: function (data) {
                                if(data.code == 1){
                                    $.messager.popup(data.msg,"success");
                                    that.loadData();
                                }else{
                                    $.messager.popup(data.msg,'error');
                                }
                            }
                        })
                    });
                }else{
                    $.messager.popup('请选择一条记录后再删除','warn');
                }
            },
            showDiv: function (div) {
                var divs = ['main_div', 'edit_div', 'accountview_div', 'formula_div'];
                for(var i=0; i<divs.length; i++){
                    $("#" + divs[i]).hide();
                }
                $("#" + div).show();
            }
        }
    });


    BASE.ProfitprofitReportItem.init();
});